package com.vdm.action;

import com.vdm.model.CompanyInfo;
import com.vdm.model.UserInfo;
import com.vdm.service.CompanyService;
import com.vdm.util.Constants;
import com.vdm.util.ErrorConstants;
import com.vdm.util.ReturnMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "company")
public class CompanyController {

    @Autowired
    CompanyService companyService;

    @RequestMapping(value = "/toCompanyList")
    public String toCompanyList(){return "company/companyList";}

    @ResponseBody
    @RequestMapping(value = "/companyList",method = RequestMethod.GET)
    public Map<String,Object> companyList(){
        List<CompanyInfo> userList = companyService.companyInfoList();
        int count = companyService.getCompanyInfoCount();
        Map<String,Object> retMap = new HashMap<String,Object>();
        retMap.put("code",0);
        retMap.put("msg","");
        retMap.put("count",count);
        retMap.put("data",userList);
        return retMap;
    }

    @ResponseBody
    @RequestMapping(value="/updateCompanyStatus",method=RequestMethod.POST)
    public Map<String,Object> updateCompanyStatus(@RequestParam(value = "list[]")List<Integer> ids, Integer type){
        if(companyService.updateCompanyInfoStatus(ids, type)>0){
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    @ResponseBody
    @RequestMapping(value="/updateCompanyByPk",method=RequestMethod.POST)
    public Map<String,Object> updateCompanyByPk(CompanyInfo companyInfo){
        if(companyService.updateByPk(companyInfo)>0){
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    @RequestMapping(value = "/toAddCompany")
    public String toAddCompany(){return "company/addCompany";}

    @RequestMapping(value = "/addCompany" , method = RequestMethod.POST)
    public String addCompany(CompanyInfo companyInfo, HttpSession session){
        UserInfo user = (UserInfo) session.getAttribute(Constants.CURRENT_USER);
        companyInfo.setCreateBy(user.getLoginName());
        companyService.insertSelective(companyInfo);
        return "redirect:toCompanyList";
    }


}
